#!/sbin/openrc-run
# Copyright 2018 Laurent Bercot
# Distributed under the terms of the ISC License.
#
# OpenRC is only used here to trigger the s6 mechanisms.

depend() {
	need s6
}

start() {
	ebegin "Starting utmpd and wtmpd services"
	mkdir -p -m 0755 /run/utmps
	chown utmp:utmp /run/utmps

	# OpenRC has no readiness notification framework, so it can run this before s6 is ready.
	# To avoid the race (yes, I have hit it), do a polling check here.
	# If you want to avoid unnecessary delays, switch to a real service manager like s6-rc.
	until test -e /run/service/.s6-svscan/control ; do sleep 1 ; done

	ln -nsf /var/lib/utmps/services/utmpd /run/service/utmpd
	ln -nsf /var/lib/utmps/services/wtmpd /run/service/wtmpd
	s6-svlisten -U -t 5000 -- /var/lib/utmps/services/utmpd /var/lib/utmps/services/wtmpd "" s6-svscanctl -an /run/service
	eend $?
}

stop() {
	ebegin "Stopping utmpd and wtmpd services"
	rm -f /run/service/utmpd /run/service/wtmpd
	s6-svlisten -d -t 5000 -- /var/lib/utmps/services/utmpd /var/lib/utmps/services/wtmpd "" s6-svscanctl -an /run/service
	eend $?
}
